package com.wuzheng.wisgo.sanitation.cloud.repository.builder.job;

import com.wuzheng.wisgo.sanitation.framework.util.StringUtils;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author：hoipang
 * @date：Created in 2019-12-10
 * @description：构建作业道路sql
 * @modified By：
 * @version: 1.0
 */
public class JobRoadBuilder {
    /**
     * 根据条件构建查询作业道路信息的sql语句
     *
     * @param id
     * @param name
     * @param code
     * @return
     */
    public String buildConditionOfJobRoadDTOSql(String id, String name, String code) {
        return new SQL() {
            {
                SELECT("jr.id, jr.road_name AS roadName, jr.road_start AS startName,jr.road_start_long AS startLong");
                SELECT("jr.road_start_lat AS startLat, jr.road_end AS endName, jr.road_end_long AS endLong");
                SELECT("jr.road_end_lat AS endLat, jr.road_length AS roadLength, jr.road_width AS roadWidth");
                SELECT("jr.lane_type_id AS laneType, jr.lane_type AS laneTypeName, jr.gps_features AS gpsFeatures");
                SELECT("jr.gps_offset AS gpsOffset, jr.remark");
                SELECT("array_to_string(ARRAY(select jp.job_type_id from ba_job_plan_road jpr join ba_job_plan jp on jpr.plan_id = jp.id where jpr.road_id = jr.id),',') job_type");
                SELECT("CASE CAST(FLOOR(random()*10) AS INT) WHEN 9 THEN 'finished' WHEN 0 THEN 'unfinished' ELSE 'working' END AS status");
                FROM("ba_job_road AS jr");
                WHERE("jr.de_flag = FALSE");
                if (StringUtils.isNotBlank(id)) {
                    WHERE("jr.id = #{id}");
                }
                if (StringUtils.isNotBlank(name)) {
                    WHERE("jr.road_name LIKE CONCAT('%', #{name}, '%')");
                }
                if (StringUtils.isNotBlank(code)) {
                    WHERE("jr.lane_type_id = #{code}");
                }
            }
        }.toString();

    }

    /**
     * 查询作业道路状态
     * @return
     */
    public String buildListRoadStateDTOSql(){
        return new SQL () {
            {
                SELECT("jr.id,jr.gps_features AS gpsFeatures");
                SELECT("array_to_string(ARRAY(select jp.job_type_id from ba_job_plan_road jpr join ba_job_plan jp on jpr.plan_id = jp.id where jpr.road_id = jr.id),',') job_type");
                SELECT("CASE CAST(FLOOR(random()*10) AS INT) WHEN 9 THEN 'finished' WHEN 0 THEN 'unfinished' ELSE 'working' END AS status");
                FROM("ba_job_road AS jr");
                WHERE("jr.de_flag = FALSE");
            }
        }.toString();
    }
}
